package toolsimpl

import (
	"context"
	"go-caipu/pkg/services/admin/menu"
	"go-caipu/pkg/services/tools/models"
	"go-caipu/pkg/setting"
	"gorm.io/gorm"
	"strings"
)

type Service struct {
	store       *gorm.DB
	cfg         *setting.Cfg
	menuService menu.Service
}

func ProvideService(db *gorm.DB, cfg *setting.Cfg, menuService menu.Service) (*Service, error) {
	s := &Service{
		store:       db,
		cfg:         cfg,
		menuService: menuService,
	}
	return s, nil
}

func (s Service) GenToDB(tableId int) error {
	var tab models.SysTables
	var err error
	tab.TableId = tableId
	tab, err = tab.Get(s.store, false)
	if err != nil {
		return err
	}
	tab.MLTBName = strings.Replace(tab.TBName, "_", "-", -1)
	//nav menu
	mainMenu := menu.Menu{
		MenuName:  tab.ClassName + "Manage",
		Title:     tab.TableComment,
		Icon:      "ep:cpu",
		Path:      "/" + tab.MLTBName,
		MenuType:  1,
		Action:    "无",
		ParentId:  0,
		NoCache:   false,
		Component: "/" + tab.MLTBName,
		Sort:      0,
		Visible:   true,
		IsFrame:   false,
	}
	s.menuService.Insert(context.Background(), mainMenu)
	//列表
	Cmenu := menu.Menu{}
	Cmenu.MenuName = tab.ClassName + "Manage"
	Cmenu.Title = tab.TableComment
	Cmenu.Icon = "ep:cpu"
	Cmenu.Path = "/" + tab.PackageName + "/" + tab.MLTBName
	Cmenu.MenuType = 2
	Cmenu.Action = "无"
	Cmenu.Permission = tab.PackageName + ":" + tab.BusinessName + ":list"
	Cmenu.ParentId = mainMenu.MenuId
	Cmenu.NoCache = false
	Cmenu.Component = "/" + tab.PackageName + "/" + tab.MLTBName + "/index"
	Cmenu.Sort = 0
	Cmenu.Visible = true
	Cmenu.IsFrame = false
	Cmenu.CreateBy = 1
	Cmenu.UpdateBy = 1
	s.menuService.Insert(context.Background(), Cmenu)
	//search button
	MList := menu.Menu{}
	MList.MenuName = ""
	MList.Title = "分页获取" + tab.TableComment
	MList.Icon = ""
	MList.Path = tab.TBName
	MList.MenuType = 3
	MList.Action = "无"
	MList.Permission = tab.PackageName + ":" + tab.BusinessName + ":query"
	MList.ParentId = Cmenu.MenuId
	MList.NoCache = false
	MList.Sort = 0
	MList.Visible = true
	MList.IsFrame = false
	MList.CreateBy = 1
	MList.UpdateBy = 1
	s.menuService.Insert(context.Background(), MList)
	//create menu button
	MCreate := menu.Menu{}
	MCreate.MenuName = ""
	MCreate.Title = "创建" + tab.TableComment
	MCreate.Icon = ""
	MCreate.Path = tab.TBName
	MCreate.MenuType = 3
	MCreate.Action = "无"
	MCreate.Permission = tab.PackageName + ":" + tab.BusinessName + ":add"
	MCreate.ParentId = Cmenu.MenuId
	MCreate.NoCache = false
	MCreate.Sort = 0
	MCreate.Visible = true
	MCreate.IsFrame = false
	MCreate.CreateBy = 1
	MCreate.UpdateBy = 1
	s.menuService.Insert(context.Background(), MCreate)
	//edit menu button
	MUpdate := menu.Menu{}
	MUpdate.MenuName = ""
	MUpdate.Title = "修改" + tab.TableComment
	MUpdate.Icon = ""
	MUpdate.Path = tab.TBName
	MUpdate.MenuType = 3
	MUpdate.Action = "无"
	MUpdate.Permission = tab.PackageName + ":" + tab.BusinessName + ":edit"
	MUpdate.ParentId = Cmenu.MenuId
	MUpdate.NoCache = false
	MUpdate.Sort = 0
	MUpdate.Visible = false
	MUpdate.IsFrame = false
	MUpdate.CreateBy = 1
	MUpdate.UpdateBy = 1
	s.menuService.Insert(context.Background(), MUpdate)
	//delete menu button
	MDelete := menu.Menu{}
	MDelete.MenuName = ""
	MDelete.Title = "删除" + tab.TableComment
	MDelete.Icon = ""
	MDelete.Path = tab.TBName
	MDelete.MenuType = 3
	MDelete.Action = "无"
	MDelete.Permission = tab.PackageName + ":" + tab.BusinessName + ":remove"
	MDelete.ParentId = Cmenu.MenuId
	MDelete.NoCache = false
	MDelete.Sort = 0
	MDelete.Visible = false
	MDelete.IsFrame = false
	MDelete.CreateBy = 1
	MDelete.UpdateBy = 1
	s.menuService.Insert(context.Background(), MDelete)

	return nil
}
